package com.xysq.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.rockcent.action.AppAction;
import com.rockcent.action.CallbackListener;
import com.rockcent.action.impl.AppActionImpl;
import com.rockcent.model.CouponModel;
import com.rockcent.model.Topics;
import com.umeng.analytics.MobclickAgent;
import com.xysq.XYApplication;
import com.xysq.activity.LoginActivity;
import com.xysq.lemon.R;
import com.xysq.util.PropertiesUtil;
import com.xysq.util.ToastUtil;
import com.xysq.util.UseUtil;
import com.xysq.util.UserInfoKeeper;

import net.soulwolf.widget.ratiolayout.widget.RatioImageView;

import java.util.List;

/**
 * Created by Steward_Han on 2015/8/31.
 */
public class TopicAndCouponAdapter extends BaseAdapter{
    private Context context = null;
    private List<Topics> topicsList = null;
    private List<CouponModel> couponModelList = null;
    private ImageLoader imageLoader;
    private AppAction appAction;
    private String viewHolderTopicName;
    private String viewHolderCouponName;

    public TopicAndCouponAdapter(Context context, List<Topics> topicsList, List<CouponModel> couponModelList) {
        this.context = context;
        this.topicsList = topicsList;
        this.couponModelList = couponModelList;
        this .imageLoader = XYApplication.getImageLoader();
        this.appAction = AppActionImpl.getInstance(context, PropertiesUtil.getXyapiAddress(), PropertiesUtil.getQkapiAddress());
    }

    @Override
    public int getCount() {
        int count = 0;
        if (null != topicsList & null != couponModelList) {
            count = topicsList.size() + couponModelList.size();
        }
        return count;
    }

    @Override
    public Object getItem(int position) {
        CouponModel itemCoupon = null;
        Topics itemTopic = null;
        if (null != itemCoupon & itemTopic != null) {
            if(position < topicsList.size()){
                itemTopic = topicsList.get(position);
                return itemTopic;
            }else {
                itemCoupon = couponModelList.get(position - topicsList.size());
                return  itemCoupon;
            }
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        if(position < topicsList.size()){
            return position;
        }else {
            return position - topicsList.size();
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position < topicsList.size()) {//这种情况说明是专题
            ViewHolderTopic viewHolderTopic = new ViewHolderTopic();
            if (null == convertView) {
                LayoutInflater mInflater = LayoutInflater.from(context);
                convertView = mInflater.inflate(R.layout.item_homepage, null);
                int screenWidth = UseUtil.getScreenWidth((Activity) context);
                viewHolderTopic.picImg = (RatioImageView) convertView.findViewById(R.id.img_pic);
                viewHolderTopic.picImg.setMinimumHeight(screenWidth / 5 * 3);
                viewHolderTopic.likeCountTxt = (TextView) convertView.findViewById(R.id.txt_like_count);
                viewHolderTopic.heartImg = (ImageView) convertView.findViewById(R.id.img_heart);
                viewHolderTopic.behaviorLayout = (LinearLayout) convertView.findViewById(R.id.layout_behavior);
                viewHolderTopic.titleTxt = (TextView) convertView.findViewById(R.id.txt_title);
                convertView.setTag(viewHolderTopic);
                viewHolderTopicName = convertView.getTag().getClass().getName();
            } else {
                if(viewHolderTopicName != null && convertView.getTag().getClass().getName().equals(viewHolderTopicName)) {
                    viewHolderTopic = (ViewHolderTopic) convertView.getTag();
                }else {
                    LayoutInflater mInflater = LayoutInflater.from(context);
                    convertView = mInflater.inflate(R.layout.item_homepage, null);
                    int screenWidth = UseUtil.getScreenWidth((Activity) context);
                    viewHolderTopic.picImg = (RatioImageView) convertView.findViewById(R.id.img_pic);
                    viewHolderTopic.picImg.setMinimumHeight(screenWidth / 5 * 3);
                    viewHolderTopic.likeCountTxt = (TextView) convertView.findViewById(R.id.txt_like_count);
                    viewHolderTopic.heartImg = (ImageView) convertView.findViewById(R.id.img_heart);
                    viewHolderTopic.behaviorLayout = (LinearLayout) convertView.findViewById(R.id.layout_behavior);
                    viewHolderTopic.titleTxt = (TextView) convertView.findViewById(R.id.txt_title);
                    convertView.setTag(viewHolderTopic);
                    viewHolderTopicName = convertView.getTag().getClass().getName();
                }
            }
            final Topics topics = topicsList.get(position);
            if (null != topics) {
                viewHolderTopic.picImg.setTag(topics.getPicUrl());
                viewHolderTopic.titleTxt.setText(topics.getName());
                final String picUrl[] = split(topics.getPicUrl());
                imageLoader.displayImage(PropertiesUtil.getImageServer() + picUrl[0], viewHolderTopic.picImg);
                if ((UserInfoKeeper.readIsLike(context, topics.getUuid()).equals("false"))) {
                    viewHolderTopic.heartImg.setBackgroundResource(R.drawable.ic_love_hollow_small);
                } else if (UserInfoKeeper.readIsLike(context, topics.getUuid()).equals("true")) {
                    viewHolderTopic.heartImg.setBackgroundResource(R.drawable.ic_love_solid_small);
                } else {
                    ToastUtil.showLong(context, "获取用户是否已经喜欢失败");
                }
                viewHolderTopic.likeCountTxt.setText(String.valueOf(topics.getLikeCount()));
                final ViewHolderTopic finalViewHolderTopic = viewHolderTopic;
                viewHolderTopic.behaviorLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(final View view) {
                        MobclickAgent.onEvent(context, "1005");
                        if ((UserInfoKeeper.readIsLike(context, topics.getUuid()).equals("false"))) {
                            if (TextUtils.isEmpty(UserInfoKeeper.readPhoneNumber(context))) {
                                context.startActivity(new Intent(context, LoginActivity.class));
                            } else {
                                appAction.behavior(topics.getUuid(), UserInfoKeeper.readCustomerId(context), "2", new CallbackListener<Void>() {
                                    @Override
                                    public void onSuccess(Void data) {
                                        UserInfoKeeper.writeIsLike(context, "true", topics.getUuid());
                                        finalViewHolderTopic.heartImg.setBackgroundResource(R.drawable.ic_love_solid_small);
                                        topics.setLikeCount(topics.getLikeCount() + 1);
                                        notifyDataSetChanged();
                                    }

                                    @Override
                                    public void onFailure(String errorCode, String message) {
                                        ToastUtil.showShort(context, "不好意思您的love操作失败了");
                                    }
                                });
                            }
                        } else if (UserInfoKeeper.readIsLike(context, topics.getUuid()).equals("true")) {
                            ToastUtil.showLong(context, "您已经love了该专题");
                        }
                    }
                });
                return convertView;
            }
        } else {//这种情况说明是商品
            if(position > topicsList.size()) {
                ViewHolderCoupon viewHolderCoupon = new ViewHolderCoupon();
                if (null == convertView) {
                    LayoutInflater mInflater = LayoutInflater.from(context);
                    convertView = mInflater.inflate(R.layout.item_coupon, null);
                    int screenWidth = UseUtil.getScreenWidth((Activity) context);
                    viewHolderCoupon.picImg = (ImageView) convertView.findViewById(R.id.img_pic);
                    viewHolderCoupon.picImg.setMinimumHeight(screenWidth / 5 * 3);
                    viewHolderCoupon.priceTxt = (TextView) convertView.findViewById(R.id.txt_price);
                    viewHolderCoupon.nameTxt = (TextView) convertView.findViewById(R.id.txt_title);
                    convertView.setTag(viewHolderCoupon);
                    viewHolderCouponName = convertView.getTag().getClass().getName();
                } else {
                    if (viewHolderCouponName != null && viewHolderCouponName.equals(convertView.getTag().getClass().getName())) {
                        viewHolderCoupon = (ViewHolderCoupon) convertView.getTag();
                    } else {
                        LayoutInflater mInflater = LayoutInflater.from(context);
                        convertView = mInflater.inflate(R.layout.item_coupon, null);
                        int screenWidth = UseUtil.getScreenWidth((Activity) context);
                        viewHolderCoupon.picImg = (ImageView) convertView.findViewById(R.id.img_pic);
                        viewHolderCoupon.picImg.setMinimumHeight(screenWidth / 5 * 3);
                        viewHolderCoupon.priceTxt = (TextView) convertView.findViewById(R.id.txt_price);
                        viewHolderCoupon.nameTxt = (TextView) convertView.findViewById(R.id.txt_title);
                        convertView.setTag(viewHolderCoupon);
                        viewHolderCouponName = convertView.getTag().getClass().getName();
                    }
                }

                final CouponModel couponModel = couponModelList.get(position - topicsList.size() - 1);//减1是为了去除增加“推荐货品”的影响，点击事件也要考虑此影响。
                if (null != couponModel) {
                    viewHolderCoupon.priceTxt.setText("￥" + UseUtil.handlePrice(couponModel.getEstimateAmount()));
                    imageLoader.displayImage(PropertiesUtil.getImageServer() + couponModel.getLogoUrl(), viewHolderCoupon.picImg);
                    viewHolderCoupon.nameTxt.setText(String.valueOf(couponModel.getName()));
                }
            }else {
                LayoutInflater mInflater = LayoutInflater.from(context);
                convertView = mInflater.inflate(R.layout.item_recommend, null);
                convertView.setTag(new Object());
            }
        }
        return convertView;
    }

    private static class ViewHolderTopic {
        RatioImageView picImg;
        TextView likeCountTxt;
        ImageView heartImg;
        LinearLayout behaviorLayout;
        TextView titleTxt;
    }

    private static class ViewHolderCoupon{
        ImageView picImg;
        TextView nameTxt;
        TextView priceTxt;
    }

    private String[] split(String picUrls) {
        String[] picUrl = picUrls.split(";");
        return picUrl;
    }
}
